#!/usr/bin/env bash

DIR=$(git rev-parse --show-toplevel)

# ARGUMENTS
SIPIFY=NO
while getopts ":s" opt; do
  case $opt in
    s)
      # sipify header
      SIPIFY=YES
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
  esac
done
shift $(($OPTIND - 1))



for file in $*; do
  d=${file#*/}
  d=${d%/*}
  f=${file##*/}
  f=${f%.*}
  header="src/$d/$f.h"

  if ! grep -Fxq "$d/$f.sip" python/auto_sip.blocklist; then
    echo -e "\033[0;31m$d/$f.sip is an automatically generated SIP file\033[0m"
    echo -e "  g) \x1B[4mg\x1B[0menerate the SIP file \033[0;32m./scripts/sipify.pl $header > python/$d/$f.sip\033[0m"
    echo -e "  s) \x1B[4ms\x1B[0mhow the diff"
    SHOW=NO
    while read -n 1 n; do
      echo ""
      case $n in
        g)
          echo "Generating the SIP file ..."
          pushd ${DIR}
          ./scripts/sipify.pl $header > python/$d/$f.sip
          popd
          break
          ;;
        s)
          SHOW=YES
          break
          ;;
        *)
         invalid option
         ;;
      esac
    done
    if [[ $SHOW =~ NO ]]; then
      continue
    fi
  fi

  if [[ $SIPIFY =~ YES ]]; then
    tempfile=$(mktemp ${DIR}/${f}XXXX --suffix=.h)
    ${DIR}/scripts/sipify.pl ${DIR}/$header > $tempfile
  else
    tempfile=$header
  fi
  vimdiff $tempfile python/$d/$f.sip

done
